﻿using System.Xml;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.TDuResource
{
    class TDuSceneCfgReader
    {

        public TDuSceneCfgReader()
        {

        }
        //读取Scene节点的属性
        public SceneAttr readScene(XmlDocument xmlDocument)
        {
            SceneAttr sceneAttr;
            sceneAttr.nTimes = 0;
            sceneAttr.nFPS = 0;
            sceneAttr.nLimitErrorCount = 3;
            sceneAttr.fScores = 0;
            sceneAttr.bLoopByName = false;
            sceneAttr.strNavagation = "";
            sceneAttr.strBgSound = "";

            var SenceNode = xmlDocument.SelectSingleNode("Sence");
            if (SenceNode != null)
            {
                sceneAttr.nTimes = int.Parse(SenceNode.Attributes["time"].Value);
                sceneAttr.nLimitErrorCount = int.Parse(SenceNode.Attributes["m_nLimitErrorCount"].Value);
                if (sceneAttr.nLimitErrorCount == 0) sceneAttr.nLimitErrorCount = 10;

                sceneAttr.fScores = double.Parse(SenceNode.Attributes["scores"].Value);
                sceneAttr.bLoopByName = bool.Parse(SenceNode.Attributes["resetLoop"].Value);
                sceneAttr.strNavagation = SenceNode.Attributes["navgation"].Value;
                sceneAttr.strBgSound = SenceNode.Attributes["bgSound"].Value;
                sceneAttr.nFPS = int.Parse(SenceNode.Attributes["fps"].Value);

                if (sceneAttr.nFPS <= 0) { sceneAttr.nFPS = 30; }
            }
            return sceneAttr;
        }

        //读取Subtitle节点   string[] 类型修改为 Stack类型，使用栈存取str
        public List<string> readSubTitle(XmlDocument xmlDocument)
        {
            List<string> captions = new List<string>();

            var titles = xmlDocument.SelectSingleNode("SubTitles");
            if (titles != null)
            {
                var title = titles.SelectNodes("title");
                for (var i = 0; i < title.Count; i++)
                {
                    string str = title[i].Value;
                    if (str != "")
                        captions.Add(str);
                }
            }

            return captions;
        }

        //读取audios节点
        public List<string> readSound(XmlDocument xmlDocument)
        {
            List<string> sounds = new List<string>();

            var audios = xmlDocument.SelectSingleNode("Audios");
            if (audios != null)
            {
                var sound = audios.SelectNodes("sound");
                for (var i = 0; i < sound.Count; i++)
                {
                    string str = sound[i].InnerText;
                    if (str != "")
                        sounds.Add(str);
                }

            }
            return sounds;
        }

        //读取OperStep节点
        public List<SceneSteps> readOperStep(XmlDocument xmlDocument)
        {
            List<SceneSteps> sceneSteps = new List<SceneSteps>();

            var nodeStep = xmlDocument.SelectSingleNode("Sence/OperStep");
            if (nodeStep != null)
            {
                //var cFind = "";
                var BigStep = xmlDocument.SelectNodes("Sence/OperStep/BigStep");
                for (var i = 0; i < BigStep.Count; i++)
                {
                    SceneSteps sceneStep;
                    {
                        sceneStep.strBigStep = "";
                        sceneStep.arrSmlSteps = new List<string>();
                    }
                    sceneStep.strBigStep = BigStep[i].Attributes["name"].Value;
                    var wstr = BigStep[i].InnerText;
                    if (wstr != "")
                        sceneStep.arrSmlSteps = new List<string>(wstr.Split(','));
                    sceneSteps.Add(sceneStep);
                }
            }

            return sceneSteps;
        }

        //读取Property节点
        public Property readProperty(XmlDocument xmlDocument)
        {
            Property pts;
            pts.strNavagation = "";
            pts.strSkyBox = "";
            pts.strInfoCap = "";
            pts.strAssistPic = "";
            pts.fAniSpeed = 0;
            pts.fWalkSpeed = 0;
            pts.arrBackGround = new float[4];

            pts.bAnimationState = false;
            pts.bMultiViews = false;
            pts.bCaption = false;
            pts.bSound = false;
            pts.bBgColor = false;
            pts.bShowVP = false;
            pts.bFindView = false;
            pts.bHavePic = false;
            pts.bAllowThrow = false;
            pts.bHaveRota = false;
            pts.bToolAutoBack = false;
            pts.nHintPos = 0;
            pts.nBigPos = 0;
            pts.nSmallPos = 0;
            pts.arrObjRGBA = new int[4];
            pts.ePracticeMode = PracticeMode.E_PRACTICE;
            pts.eSceneType = SceneType.E_SIMULATION;
            pts.transparentObj = "";

            var vPropertys = xmlDocument.SelectSingleNode("Sence/Propertys");
            if (vPropertys != null)
            {
                var navgation = vPropertys.SelectSingleNode("Navgation");
                if (navgation != null)
                {
                    pts.strNavagation = navgation.InnerText;
                    var speed = navgation.Attributes["speed"];
                    if (speed != null)
                    {
                        pts.fWalkSpeed = float.Parse(speed.Value);
                        if (pts.fWalkSpeed < 0.0001)
                        {
                            pts.fWalkSpeed = 1;
                        }
                    }

                }

                var SkyBox = vPropertys.SelectSingleNode("SkyBox");
                if (SkyBox != null)
                    pts.strSkyBox = SkyBox.InnerText;

                var InfoWin = vPropertys.SelectSingleNode("InfoWin");
                if (InfoWin != null)
                    pts.strInfoCap = InfoWin.InnerText;

                var AssistPic = vPropertys.SelectSingleNode("AssistPic");
                if (AssistPic != null)
                    pts.strAssistPic = AssistPic.InnerText;

                var AnimationSpeed = vPropertys.SelectSingleNode("AnimationSpeed");
                if (AnimationSpeed != null)
                {
                    pts.fAniSpeed = int.Parse(AnimationSpeed.InnerText);
                }

                var bgColor = vPropertys.SelectSingleNode("BackGround");
                if (bgColor != null)
                {
                    pts.arrBackGround[0] = float.Parse(bgColor.Attributes["r"].Value);
                    pts.arrBackGround[1] = float.Parse(bgColor.Attributes["g"].Value);
                    pts.arrBackGround[2] = float.Parse(bgColor.Attributes["b"].Value);
                    pts.arrBackGround[3] = float.Parse(bgColor.Attributes["a"].Value);
                }

                var AnimationState = vPropertys.SelectSingleNode("AnimationState");
                if (AnimationState != null)
                {
                    pts.bAnimationState = bool.Parse(AnimationState.InnerText);
                }

                var MultViews = vPropertys.SelectSingleNode("MultViews");
                if (MultViews != null)
                {
                    pts.bMultiViews = bool.Parse(MultViews.InnerText);
                }

                var tools = vPropertys.SelectSingleNode("Tools");
                if (tools != null)
                {
                    pts.bCaption = bool.Parse(tools.Attributes["caption"].Value);
                    pts.bSound = bool.Parse(tools.Attributes["sound"].Value);
                    pts.bBgColor = bool.Parse(tools.Attributes["bgColor"].Value);
                    pts.bShowVP = bool.Parse(tools.Attributes["showVP"].Value);
                    pts.bFindView = bool.Parse(tools.Attributes["findview"].Value);
                }

                var HaveRota = vPropertys.SelectSingleNode("HaveRota");
                if (HaveRota != null)
                {
                    pts.bHaveRota = bool.Parse(HaveRota.InnerText);
                }

                var ToolAutoBack = vPropertys.SelectSingleNode("ToolAutoBack");
                if (ToolAutoBack != null)
                {
                    pts.bToolAutoBack = bool.Parse(ToolAutoBack.InnerText);
                }
                var HintPos = vPropertys.SelectSingleNode("HintPos");
                if (HintPos != null)
                {
                    pts.nHintPos = int.Parse(HintPos.InnerText);
                }
                var BigPos = vPropertys.SelectSingleNode("BigPos");
                if (BigPos != null)
                {
                    pts.nBigPos = int.Parse(BigPos.InnerText);
                }
                var nSmallPos = vPropertys.SelectSingleNode("nSmallPos");
                if (nSmallPos != null)
                {
                    pts.nSmallPos = int.Parse(nSmallPos.Value);
                }

                var ActObjRGBA = vPropertys.SelectSingleNode("ActObjRGBA");
                if (ActObjRGBA != null)
                {
                    if (ActObjRGBA.InnerText == "")
                        ActObjRGBA.InnerText = "255,37,37,100";
                    if (ActObjRGBA.InnerText != "")
                    {
                        var vecRGBA = ActObjRGBA.InnerText.Split(',');
                        for (var i = 0; i < vecRGBA.Length; i++)
                        {
                            if (i < 3)
                            {
                                pts.arrObjRGBA[i] = int.Parse(vecRGBA[i])*1/255;
                            }
                            else
                            {
                                pts.arrObjRGBA[i] = int.Parse(vecRGBA[i])*1/100;
                            }
                        }
                    }
                }

                var strTypeNode = vPropertys.SelectSingleNode("SenceType");
                if (strTypeNode != null)
                {
                    var strType = strTypeNode.InnerText;
                    if (strType == "Assemble")
                    {
                        pts.eSceneType = SceneType.E_ASSEMBLE;
                    }
                    else if (strType == "Dismantle")
                    {
                        pts.eSceneType = SceneType.E_DISMANTLE;
                    }
                    else if (strType == "Simulation")
                    {
                        pts.eSceneType = SceneType.E_SIMULATION;
                    }
                    else
                    {
                        pts.eSceneType = SceneType.E_SHOW;
                    }

                }

                var TransparentObj = vPropertys.SelectSingleNode("TransparentObj");
                if (TransparentObj != null)
                {
                    pts.transparentObj = TransparentObj.InnerText;
                }

                if (pts.strAssistPic == "true")
                {
                    pts.bHavePic = true;
                }
            }
            return pts;
        }

        //读取BipAnimation节点
        public List<BipAnimation> readBipAnimation(XmlDocument xmlDocument)
        {
            List<BipAnimation> bipAnimations = new List<BipAnimation>();

            var _bipAnimation = xmlDocument.SelectSingleNode("Sence/BipAnimation");
            if (_bipAnimation != null)
            {
                var skeleton = _bipAnimation.SelectNodes("Skeleton");

                for (int i = 0; i < skeleton.Count; i++)
                {
                    BipAnimation bipAnimation;
                    bipAnimation.strBipName = "";
                    bipAnimation.strBipChild = "";

                    var strText = skeleton[i].SelectSingleNode("RootName").InnerText;
                    if (strText != "")
                        bipAnimation.strBipName = strText;

                    strText = skeleton[i].SelectSingleNode("ChildName").InnerText;
                    if (strText != "")
                        bipAnimation.strBipChild = strText;

                    bipAnimations.Add(bipAnimation);
                }
            }
            return bipAnimations;
        }

        //读取ToolsTranslate节点
        public List<ToolsTranslate> readToolsTranslate(XmlDocument xmlDocument)
        {
            List<ToolsTranslate> toolsTranslates = new List<ToolsTranslate>();

            var _tool = xmlDocument.SelectSingleNode("Sence/ToolsTranslate");
            if (_tool != null)
            {

                var toolNode = _tool.SelectNodes("Tool");

                for (var i = 0; i < toolNode.Count; i++)
                {
                    ToolsTranslate toolTranslate;
                    toolTranslate.strToolChineseName = "";
                    toolTranslate.strToolEnglishName = "";
                    toolTranslate.strToolImage = "";

                    var strText = toolNode[i].SelectSingleNode("ChineseName").InnerText;
                    if (strText != "")
                        toolTranslate.strToolChineseName = strText;

                    strText = toolNode[i].SelectSingleNode("EnglishName").InnerText;
                    if (strText != "")
                        toolTranslate.strToolEnglishName = strText;

                    strText = toolNode[i].SelectSingleNode("Image").InnerText;
                    if (strText != "")
                        toolTranslate.strToolImage = strText;

                    toolsTranslates.Add(toolTranslate);
                }
            }

            return toolsTranslates;
        }

        //读取Billboard节点
        public List<string> readBillboards(XmlDocument xmlDocument)
        {
            List<string> setBillboards = new List<string>();
            setBillboards.Clear();

            var billboardNodes = xmlDocument.SelectSingleNode("Sence/Billboards");
            if (billboardNodes != null)
            {
                var billboardNode = billboardNodes.SelectNodes("Billboard");
                for (var i = 0; i < billboardNode.Count; i++)
                {
                    string strText = billboardNode[i].InnerText;
                    if (strText != "")
                        setBillboards.Add(strText);
                }
            }
            return setBillboards;
        }

        //读取Reflection节点;
        public List<string> readReflections(XmlDocument xmlDocument)
        {
            List<string> setReflections = new List<string>();
            setReflections.Clear();

            var reflectionNodes = xmlDocument.SelectSingleNode("Sence/Reflections");
            if (reflectionNodes != null)
            {
                var reflectionNode = reflectionNodes.SelectNodes("Reflection");
                for (var i = 0; i < reflectionNode.Count; i++)
                {
                    string strText = reflectionNode[i].InnerText;
                    if (strText != "")
                        setReflections.Add(strText);
                }
            }

            return setReflections;
        }

        public List<SceneSprite> readSceneSprites(XmlDocument xmlDocument)
        {
            List<SceneSprite> scenesprites = new List<SceneSprite>();
            var sprites = xmlDocument.SelectSingleNode("Sence/Sprites");

            if (sprites != null)
            {
                //Console.WriteLine("strText");
                var spriteNode = sprites.SelectNodes("Sprite");
                for (int i = 0; i < spriteNode.Count; i++)
                {
                    SceneSprite scenesprite;
                    scenesprite.strSpriteName = "";
                    scenesprite.strImgUrl = "";
                    scenesprite.nImgHeight = 0;
                    scenesprite.nImgWidth = 0;
                    scenesprite.nSpriteWidth = 0;
                    scenesprite.nSpriteHeight = 0;
                    scenesprite.nPlantDistance = 0;
                    scenesprite.strReferenceMesh = "";
                    scenesprite.nPlantNumber = 0;

                    scenesprite.strSpriteName = spriteNode[i].Attributes["name"].Value;
                    scenesprite.strReferenceMesh = spriteNode[i].Attributes["referenceMesh"].Value;
                    scenesprite.strImgUrl = spriteNode[i].Attributes["imgUrl"].Value;
                    scenesprite.nSpriteWidth = double.Parse(spriteNode[i].Attributes["width"].Value);
                    scenesprite.nSpriteHeight = double.Parse(spriteNode[i].Attributes["height"].Value);
                    scenesprite.nImgHeight = double.Parse(spriteNode[i].Attributes["imgHeight"].Value);
                    scenesprite.nImgWidth = double.Parse(spriteNode[i].Attributes["imgWidth"].Value);
                    scenesprite.nPlantNumber = double.Parse(spriteNode[i].Attributes["plantNumber"].Value);
                    scenesprite.nPlantDistance = double.Parse(spriteNode[i].Attributes["plantDistance"].Value);

                    scenesprites.Add(scenesprite);
                }
            }

            return scenesprites;
        }

        //读取project节点
        public ProjectConfig? readProject(XmlDocument xmlDocument)
        {
            var projectConfig = xmlDocument.SelectSingleNode("Sence/ProjectConfig");
            if (projectConfig != null)
            {
                ProjectConfig arrProject;
                arrProject.strProjectName = "";
                arrProject.strBGPicturePath = "";
                arrProject.strSchoolName = "";
                arrProject.strCollegeName = "";
                arrProject.strCourseName = "";
                arrProject.strProfessionCode = "";
                arrProject.strProfessionName = "";
                arrProject.strPrincipalName = "";
                arrProject.strProjectMembers = "";
                arrProject.strProjectDetail = "";

                if (projectConfig.Attributes["ProjectName"] != null)
                {
                    arrProject.strProjectName = projectConfig.Attributes["ProjectName"].Value;
                }
                var child = projectConfig.SelectSingleNode("ProjectDescribe");
                if (child != null)
                {
                    string strText = child.SelectSingleNode("BackGroundPicture").InnerText;
                    if (strText != "")
                        arrProject.strBGPicturePath = strText;

                    strText = child.SelectSingleNode("SchoolName").InnerText;
                    if (strText != "")
                        arrProject.strSchoolName = strText;

                    strText = child.SelectSingleNode("CollegeName").InnerText;
                    if (strText != "")
                        arrProject.strCollegeName = strText;

                    strText = child.SelectSingleNode("CourseName").InnerText;
                    if (strText != "")
                        arrProject.strCourseName = strText;

                    strText = child.SelectSingleNode("ProfessionName").InnerText;
                    if (strText != "")
                        arrProject.strProfessionName = strText;

                    strText = child.SelectSingleNode("PrincipalName").InnerText;
                    if (strText != "")
                        arrProject.strPrincipalName = strText;

                    strText = child.SelectSingleNode("ProjectMembers").InnerText;
                    if (strText != "")
                        arrProject.strProjectMembers = strText;

                    strText = child.SelectSingleNode("ProjectDetail").InnerText;
                    if (strText != "")
                        arrProject.strProjectDetail = strText;

                    var pCode = child.SelectSingleNode("ProfessionName");
                    if (pCode != null)
                    {
                        if (pCode.Attributes["code"] != null)
                        {
                            arrProject.strProfessionCode = pCode.Attributes["code"].Value;
                        }
                    }
                }

                return arrProject;
            }
            else
                return null;
        }

        //读取MaterialInfo节点;
        public MusicOfficeInfo readMusicOfficeInfo(XmlDocument xmlDocument)
        {
            MusicOfficeInfo musicOfficeInfo;
            musicOfficeInfo.dReverbTime = 0.0;
            musicOfficeInfo.dVolume = 0.0;
            musicOfficeInfo.airAbsorptions = new List<AirAbsorptions>();
            musicOfficeInfo.vecMaterials = new List<Material>();
            musicOfficeInfo.vecReplacedMaterial = new List<MaterialReplacedRecord>();
            musicOfficeInfo.vecExecutedAnimation = new List<AnimationExecutedRecord>();

            var musicOfficeElment = xmlDocument.SelectSingleNode("Sence/MusicOfficeInfo");
            if (musicOfficeElment != null)
            {
                musicOfficeInfo.dVolume = double.Parse(musicOfficeElment.Attributes["volume"].Value);
                musicOfficeInfo.dReverbTime = double.Parse(musicOfficeElment.Attributes["decayTime"].Value);
            }

            var materialInfo = xmlDocument.SelectSingleNode("MaterialInfos");
            if (materialInfo != null)
            {
                var materailElements = materialInfo.SelectNodes("Material");
                for (int i = 0; i < materailElements.Count; i++)
                {
                    Material material;
                    material.strMaterialName = "";
                    material.strMaterialID = "";
                    material.dMaterialArea = 0;
                    material.vecAbsorptions = new List<Absorption>();

                    material.strMaterialName = materailElements[i].Attributes["name"].Value;
                    material.strMaterialID = materailElements[i].Attributes["id"].Value;
                    material.dMaterialArea = int.Parse(materailElements[i].Attributes["squre"].Value);

                    var absorptionElement = materailElements[i].SelectNodes("AbsorptionAbsorpitivity");
                    for (int j = 0; j < absorptionElement.Count; j++)
                    {
                        Absorption absorption;
                        absorption.dAbsorptionCoefficient = 0.0;
                        absorption.strFrequency = "";

                        absorption.strFrequency = absorptionElement[j].Attributes["frequency"].Value;
                        var strText = absorptionElement[j].InnerText;
                        if (strText != "")
                        {
                            absorption.dAbsorptionCoefficient = double.Parse(strText);
                        }

                        material.vecAbsorptions.Add(absorption);
                    }

                    musicOfficeInfo.vecMaterials.Add(material);
                }
            }

            //读取AirAbsorption
            if (musicOfficeElment != null)
                return musicOfficeInfo;

            var airAbsorptionsElements = musicOfficeElment.SelectSingleNode("AirAbsorptions");
            if (airAbsorptionsElements != null)
            {
                var AirAbsorptionElements = airAbsorptionsElements.SelectNodes("AirAbsorption");

                for (int i = 0; i < AirAbsorptionElements.Count; i++)
                {
                    AirAbsorptions airAbsorptionsTemp;
                    airAbsorptionsTemp.strFrequency = "";
                    airAbsorptionsTemp.vecHumidityAbsorptions = new List<humidityAbsorption>();

                    airAbsorptionsTemp.strFrequency = AirAbsorptionElements[i].Attributes["frequency"].Value;
                    var humidityElemtns = AirAbsorptionElements[i].SelectNodes("humidityAbsorption");
                    for (int j = 0; j < humidityElemtns.Count; j++)
                    {
                        humidityAbsorption humidityAbsorptionTemp;
                        humidityAbsorptionTemp.strHumidity = "";
                        humidityAbsorptionTemp.dHumidityAbsorption = 0.0;


                        humidityAbsorptionTemp.strHumidity = humidityElemtns[j].Attributes["humidity"].Value;
                        string strText = humidityElemtns[j].InnerText;
                        if (strText != "")
                            humidityAbsorptionTemp.dHumidityAbsorption = double.Parse(strText);

                        airAbsorptionsTemp.vecHumidityAbsorptions.Add(humidityAbsorptionTemp);
                    }

                    musicOfficeInfo.airAbsorptions.Add(airAbsorptionsTemp);
                }
            }

            //获取已更换材质列表;
            var materialRecordElements = musicOfficeElment.SelectSingleNode("MaterialRecords");
            if (materialRecordElements != null)
            {
                var materialMeshElements = materialRecordElements.SelectNodes("MaterialMesh");
                for (int i = 0; i < materialMeshElements.Count; i++)
                {
                    MaterialReplacedRecord materialReplaceRecord;
                    materialReplaceRecord.strMeshName = "";
                    materialReplaceRecord.strReplacedImage = "";

                    if (materialMeshElements[i].Attributes["meshname"] != null)
                        materialReplaceRecord.strMeshName = materialMeshElements[i].Attributes["meshname"].Value;
                    if (materialMeshElements[i].Attributes["updatedimage"] != null)
                        materialReplaceRecord.strReplacedImage = materialMeshElements[i].Attributes["updatedimage"].Value;

                    musicOfficeInfo.vecReplacedMaterial.Add(materialReplaceRecord);
                }
            }

            //获取已执行动画步骤;
            var animationRecordElements = musicOfficeElment.SelectSingleNode("AnimationRecords");
            if (animationRecordElements != null)
            {
                var animationElements = animationRecordElements.SelectNodes("Animation");
                for (int i = 0; i < animationElements.Count; i++)
                {
                    AnimationExecutedRecord animationExecutedRecord;
                    animationExecutedRecord.strStepName = "";
                    animationExecutedRecord.strActivateObj = "";

                    if (animationElements[i].Attributes["stepname"] != null)
                        animationExecutedRecord.strStepName = animationElements[i].Attributes["stepname"].Value;
                    musicOfficeInfo.vecExecutedAnimation.Add(animationExecutedRecord);
                }
            }

            return musicOfficeInfo;
        }

        //读取Viewports节点;
        public List<ViewPort> readViewports(XmlDocument xmlDocument)
        {
            List<ViewPort> arrViewports = new List<ViewPort>();

            var viewports = xmlDocument.SelectSingleNode("Sence/ViewPorts");
            if (viewports != null)
            {
                var child = viewports.SelectNodes("ViewPort");

                for (var i = 0; i < child.Count; i++)
                {
                    ViewPort viewPort;
                    viewPort.strViewPortName = "";
                    viewPort.bSlave = false;
                    viewPort.bFolder = false;
                    viewPort.arr3Pos = new float[3];
                    viewPort.arr3Rota = new float[3];
                    viewPort.vecItems = new List<ViewPort>();

                    viewPort.bSlave = bool.Parse(child[i].Attributes["slave"].Value);
                    string strText = child[i].SelectSingleNode("Name").InnerText;
                    if (strText != "")
                        viewPort.strViewPortName = strText;

                    var pos = child[i].SelectSingleNode("Pos");
                    viewPort.arr3Pos[0] = float.Parse(pos.Attributes["x"].Value);
                    viewPort.arr3Pos[1] = float.Parse(pos.Attributes["y"].Value);
                    viewPort.arr3Pos[2] = float.Parse(pos.Attributes["z"].Value);

                    var rota = child[i].SelectSingleNode("Rot");
                    viewPort.arr3Rota[0] = float.Parse(rota.Attributes["x"].Value);
                    viewPort.arr3Rota[1] = float.Parse(rota.Attributes["y"].Value);
                    viewPort.arr3Rota[2] = float.Parse(rota.Attributes["z"].Value);

                    //Debug.Log("position:" + viewPort.arr3Pos[0] + "," + viewPort.arr3Pos[1] + "," + viewPort.arr3Pos[2]);
                    //Debug.Log("rotation:" + viewPort.arr3Rota[0] + "," + viewPort.arr3Rota[1] + "," + viewPort.arr3Rota[2]);
                    arrViewports.Add(viewPort);

                }
            }

            return arrViewports;
        }

        //读取webVRConfig
        public TDuWebVRConfig readWebVRConfig(XmlDocument xmlDocument)
        {
            TDuWebVRConfig webvrConfig;
            webvrConfig.bUseMainButtonToTeleportate = true;
            webvrConfig.nDashboardSize = 2;
            webvrConfig.ntimeToTeleportate = 1000;
            webvrConfig.strDashboardName = "TDuDashboard";
            webvrConfig.vecFloorsName = new HashSet<string>();
            webvrConfig.vecScalingFactors = new double[3];
            webvrConfig.vecScalingFactors[0] = 0.0017;
            webvrConfig.vecScalingFactors[1] = 0.0017;
            webvrConfig.vecScalingFactors[2] = 0.0017;

            var webvrNode = xmlDocument.SelectSingleNode("Sence/TDuWebVR");
            if (webvrNode != null)
            {
                var scaling = webvrNode.SelectSingleNode("SceneScaling");
                if (scaling != null)
                {
                    webvrConfig.vecScalingFactors[0] = double.Parse(scaling.Attributes["x"].Value);
                    webvrConfig.vecScalingFactors[1] = double.Parse(scaling.Attributes["y"].Value);
                    webvrConfig.vecScalingFactors[2] = double.Parse(scaling.Attributes["z"].Value);
                }

                var dashboard = webvrNode.SelectSingleNode("Dashboard");
                if (dashboard != null)
                {
                    webvrConfig.strDashboardName = dashboard.Attributes["name"].Value;
                    webvrConfig.nDashboardSize = int.Parse(dashboard.Attributes["size"].Value);
                }

                var teleportation = webvrNode.SelectSingleNode("Teleportation");
                if (teleportation != null)
                {
                    webvrConfig.bUseMainButtonToTeleportate = bool.Parse(teleportation.Attributes["useMainButton"].Value);
                    webvrConfig.ntimeToTeleportate = int.Parse(teleportation.Attributes["timeToTeleportate"].Value);
                }

                var floor = webvrNode.SelectSingleNode("FloorMeshes");
                if (floor != null)
                {
                    string strText = floor.InnerText;
                    if (strText != "")
                    {
                        webvrConfig.vecFloorsName = new HashSet<string>(strText.Split(','));
                    }
                }
            }
            return webvrConfig;
        }

        //读取Nodes节点
        public List<SceneNode> readSceneNodes(XmlDocument xmlDocument)
        {
            List<SceneNode> sceneNodes = new List<SceneNode>();
            string strText;

            var nodes = xmlDocument.SelectSingleNode("Sence/Nodes");

            if (nodes != null)
            {
                var child = nodes.SelectNodes("Node");
                for (var i = 0; i < child.Count; i++)
                {
                    SceneNode senceNode;
                    senceNode.strName = "";
                    senceNode.strFile = "";
                    senceNode.strUserID = "";
                    senceNode.strModelID = "";
                    senceNode.bDynamic = false;
                    senceNode.bUsingShadow = false;
                    senceNode.nLevel = 0;
                    senceNode.vec3Size = new double[3];
                    senceNode.vec3Pos = new double[3];
                    senceNode.vec3Rota = new double[3];
                    senceNode.vec3Scale = new double[3];

                    strText = child[i].SelectSingleNode("Name").InnerText;
                    if (strText != "")
                        senceNode.strName = strText;

                    var file = child[i].SelectSingleNode("File");
                    strText = file.InnerText;
                    if (strText != "")
                        senceNode.strFile = strText;

                    senceNode.bDynamic = bool.Parse(file.Attributes["dynamic"].Value);
                    senceNode.nLevel = int.Parse(file.Attributes["level"].Value);

                    if (file.Attributes["UserID"] != null)
                        senceNode.strUserID = file.Attributes["UserID"].Value;
                    if (file.Attributes["ModelID"] != null)
                        senceNode.strModelID = file.Attributes["ModelID"].Value;

                    var size = child[i].SelectSingleNode("Size");
                    if (size != null)
                    {
                        senceNode.vec3Size[0] = double.Parse(size.Attributes["x"].Value);
                        senceNode.vec3Size[1] = double.Parse(size.Attributes["y"].Value);
                        senceNode.vec3Size[2] = double.Parse(size.Attributes["z"].Value);
                    }
                    else
                    {
                        senceNode.vec3Size[0] = 1000;
                        senceNode.vec3Size[1] = 1000;
                        senceNode.vec3Size[2] = 1000;
                    }


                    var pos = child[i].SelectSingleNode("Pos");
                    senceNode.vec3Pos[0] = double.Parse(pos.Attributes["x"].Value);
                    senceNode.vec3Pos[1] = double.Parse(pos.Attributes["y"].Value);
                    senceNode.vec3Pos[2] = double.Parse(pos.Attributes["z"].Value);

                    var rota = child[i].SelectSingleNode("Rot");
                    senceNode.vec3Rota[0] = double.Parse(rota.Attributes["x"].Value);
                    senceNode.vec3Rota[1] = double.Parse(rota.Attributes["y"].Value);
                    senceNode.vec3Rota[2] = double.Parse(rota.Attributes["z"].Value);

                    var scale = child[i].SelectSingleNode("Scale");
                    senceNode.vec3Scale[0] = double.Parse(scale.Attributes["x"].Value);
                    senceNode.vec3Scale[1] = double.Parse(scale.Attributes["y"].Value);
                    senceNode.vec3Scale[2] = double.Parse(scale.Attributes["z"].Value);


                    sceneNodes.Add(senceNode);
                }

            }

            return sceneNodes;
        }

        //读取SceneEnvironment
        public SceneEnvironment readSceneEnvironment(XmlDocument xmlDocument)
        {
            SceneEnvironment sceneEnvironment;
            sceneEnvironment.strEnvTexture = "";
            sceneEnvironment.strSkyTexture = "";
            sceneEnvironment.bUseGround = true;
            sceneEnvironment.bUseSkybox = true;
            sceneEnvironment.bUse = false;
            sceneEnvironment.arrGroundColor = new double[3];

            var nodes = xmlDocument.SelectSingleNode("Sence/Nodes");

            if (nodes != null)
            {
                if (nodes.Attributes["useEnvironment"] != null)
                    sceneEnvironment.bUse = bool.Parse(nodes.Attributes["useEnvironment"].Value);

                var child = nodes.SelectSingleNode("Environments");
                if (child != null)
                {
                    string strText = child.SelectSingleNode("skyTexture").InnerText;
                    if (strText != "")
                        sceneEnvironment.strSkyTexture = strText;

                    strText = child.SelectSingleNode("environmentTexture").InnerText;
                    if (strText != "")
                        sceneEnvironment.strEnvTexture = strText;

                    var groundColor = child.SelectSingleNode("groundColor");
                    sceneEnvironment.arrGroundColor[0] = double.Parse(groundColor.Attributes["r"].Value);
                    sceneEnvironment.arrGroundColor[1] = double.Parse(groundColor.Attributes["g"].Value);
                    sceneEnvironment.arrGroundColor[2] = double.Parse(groundColor.Attributes["b"].Value);

                    sceneEnvironment.bUseGround = bool.Parse(child.Attributes["useGround"].Value);
                    sceneEnvironment.bUseSkybox = bool.Parse(child.Attributes["useSkybox"].Value);
                }

            }

            return sceneEnvironment;
        }

        //读取Switch节点；
        public SceneSwitch readSceneSiwtch(XmlDocument xmlDocument)
        {
            SceneSwitch sceneSwitch;
            sceneSwitch.nChoice = 0;
            sceneSwitch.arrCases = new List<SwitchCase>();


            var nodes = xmlDocument.SelectSingleNode("Sence/Nodes");
            if (nodes != null)
            {
                var switchNode = nodes.SelectSingleNode("Switch");
                if (switchNode != null)
                {
                    string strText = switchNode.SelectSingleNode("Choice").InnerText;
                    if (strText != "")
                        sceneSwitch.nChoice = int.Parse(strText);
                    var caseChild = switchNode.SelectNodes("Case");
                    for (var i = 0; i < caseChild.Count; i++)
                    {
                        SwitchCase switchCase;
                        switchCase.strCaseName = "";
                        switchCase.arrNodeName = new List<string>();

                        switchCase.strCaseName = caseChild[0].Attributes["name"].Value;

                        string strCase = caseChild[0].InnerText;
                        if (strCase != "")
                            switchCase.arrNodeName = new List<string>(strCase.Split(','));
                        sceneSwitch.arrCases.Add(switchCase);
                    }
                }
            }

            return sceneSwitch;
        }

        //读取SchematicView节点;
        public List<SchematicView> readSchematicView(XmlDocument xmlDocument)
        {
            List<SchematicView> schematicViews = new List<SchematicView>();

            var svNodes = xmlDocument.SelectSingleNode("Sence/SchematicView");
            if (svNodes != null)
            {
                var schematics = svNodes.SelectNodes("Schematic");

                for (var i = 0; i < schematics.Count; i++)
                {
                    SchematicView schematic;
                    schematic.strSchematicView = "";
                    schematic.strFileName = "";
                    schematic.strUserID = "";
                    schematic.strSceneID = "";
                    schematic.bUseUI = false;

                    var filenode = schematics[i].SelectSingleNode("File");
                    string strText = filenode.InnerText;
                    if (strText != "")
                        schematic.strFileName = strText;
                    schematic.strUserID = filenode.Attributes["UserID"].Value;
                    schematic.strSceneID = filenode.Attributes["SceneID"].Value;

                    strText = schematics[i].SelectSingleNode("Name").InnerText;
                    if (strText != "")
                        schematic.strSchematicView = strText;

                    strText = schematics[i].SelectSingleNode("NeedUI").InnerText;
                    if (strText != "")
                        schematic.bUseUI = bool.Parse(strText);

                    schematicViews.Add(schematic);
                }
            }

            return schematicViews;
        }

        //读取ExplosiveView节点;
        public List<ExplosiveView> readExplosiveView(XmlDocument xmlDocument)
        {
            List<ExplosiveView> explosiveViews = new List<ExplosiveView>();

            var evNodes = xmlDocument.SelectSingleNode("Sence/ExplosiveView");
            if (evNodes != null)
            {
                var explosives = evNodes.SelectNodes("Explosive");

                for (var i = 0; i < explosives.Count; i++)
                {
                    ExplosiveView explosive;
                    explosive.strExplosiveViewName = "";
                    explosive.strFileName = "";
                    explosive.strSceneID = "";
                    explosive.strUserID = "";
                    explosive.bUseUI = false;

                    var filenode = explosives[i].SelectSingleNode("File");
                    string strText = filenode.InnerText;
                    if (strText != "")
                        explosive.strFileName = strText;
                    explosive.strUserID = filenode.Attributes["UserID"].Value;
                    explosive.strSceneID = filenode.Attributes["SceneID"].Value;

                    strText = explosives[i].SelectSingleNode("Name").InnerText;
                    if (strText != "")
                        explosive.strExplosiveViewName = strText;

                    strText = explosives[i].SelectSingleNode("NeedUI").InnerText;
                    if (strText != "")
                        explosive.bUseUI = bool.Parse(strText);

                    explosiveViews.Add(explosive);
                }
            }

            return explosiveViews;
        }

        //读取InitState节点
        public FullInitVarState readInitState(XmlDocument xmlDocument)
        {
            FullInitVarState initState;
            initState.strDescription = "";
            initState.strSubject = "";
            initState.arrVarStates = new List<InitVarState>();

            var initStateNodes = xmlDocument.SelectSingleNode("Sence/InitStates");
            if (initStateNodes != null)
            {
                initState.strDescription = initStateNodes.Attributes["disp"].Value;
                initState.strSubject = initStateNodes.Attributes["subject"].Value;

                var child = initStateNodes.SelectNodes("Variable");
                for (var i = 0; i < child.Count; i++)
                {
                    InitVarState varState;
                    varState.strName = "";
                    varState.dInitValue = 0.0;
                    varState.dDescValue = 0.0;
                    varState.dScore = 0.0;
                    varState.dDecution = 0.0;
                    varState.nStandardCount = 0;
                    varState.bDisableUpdate = false;
                    varState.bErrorStep = false;
                    // bFolder : false

                    varState.strName = child[i].Attributes["name"].Value;
                    varState.dInitValue = double.Parse(child[i].Attributes["value"].Value);
                    varState.dDescValue = double.Parse(child[i].Attributes["descValue"].Value);

                    varState.dScore = double.Parse(child[i].Attributes["score"].Value);
                    varState.nStandardCount = double.Parse(child[i].Attributes["count"].Value);
                    varState.dDecution = double.Parse(child[i].Attributes["deduction"].Value);
                    varState.bDisableUpdate = bool.Parse(child[i].Attributes["disableUpdate"].Value);

                    if (varState.strName != null && varState.strName != "")
                    {
                        initState.arrVarStates.Add(varState);
                    }
                }
            }

            return initState;
        }

        //读取每个Type分割的VariableNode
        public void readVarNodeFolder(XmlNode node, ref VariableNode variableNode, int type, object objContainer, ref Dictionary<string, VariableNode> vecVariableNodes)
        {
            for (var i = 0; i < node.ChildNodes.Count; i++)
            {
                VariableNode variableItem = new VariableNode();
                if (node.ChildNodes[i].Name == "VariableType")
                {
                    if (node.ChildNodes[i].Attributes["name"].Value == "UISet")
                        this.readVarNodeFolder(node.ChildNodes[i], ref variableNode, 1, objContainer, ref vecVariableNodes);
                    else if (node.ChildNodes[i].Attributes["name"].Value == "FreeStep")
                        this.readVarNodeFolder(node.ChildNodes[i], ref variableNode, 2, objContainer, ref vecVariableNodes);

                    this.readVarNodeFolder(node.ChildNodes[i], ref variableNode, 0, objContainer, ref vecVariableNodes);
                }

                if (node.ChildNodes[i].Name == "Variable")
                {
                    this.readVarNodeItem(node.ChildNodes[i], ref variableItem, ref vecVariableNodes);
                    if (type == 1)
                        ((List<VariableNode>)objContainer).Add(variableItem);
                    else if (type == 2)
                        ((Dictionary<string, VariableNode>)objContainer).Add(variableItem.strVarNodeName, variableItem);
                    else
                    {
                        ((Dictionary<string, VariableNode>)objContainer).Add(variableItem.strVarNodeName, variableItem);
                    }
                }
            }
        }

        //读取VariableNodes节点
        public void readVariableNodes(XmlDocument xmlDocument, ref Dictionary<string, VariableNode> vecVariableNodes, Dictionary<string, VariableNode> mapVariableNodes, List<VariableNode> arrUI, Dictionary<string, VariableNode> mapSteps)
        {
            var varnodes = xmlDocument.SelectSingleNode("Sence/VariableNodes");

            if (varnodes != null)
            {
                var childs = varnodes.ChildNodes;

                for (var j = 0; j < childs.Count; j++)
                {
                    VariableNode variableNode = new VariableNode();

                    if (childs[j].Name == "VariableType")
                    {
                        if (childs[j].Attributes["name"].Value == "UISet")
                            this.readVarNodeFolder(childs[j], ref variableNode, 1, arrUI,ref vecVariableNodes);
                        else if (childs[j].Attributes["name"].Value == "FreeStep")
                            this.readVarNodeFolder(childs[j], ref variableNode, 2, mapSteps, ref vecVariableNodes);

                        this.readVarNodeFolder(childs[j], ref variableNode, 0, mapVariableNodes, ref vecVariableNodes);
                        this.readVecNodeFolder(childs[j], ref variableNode, ref vecVariableNodes);
                        
                    }

                    if (childs[j].Name == "Variable")
                    {
                        this.readVarNodeItem(childs[j], ref variableNode, ref vecVariableNodes);
                        mapVariableNodes.Add(variableNode.strVarNodeName, variableNode);
                    }

                    //Debug.Log(variableNode.strVarNodeName);
                    if (!vecVariableNodes.ContainsKey(variableNode.strVarNodeName))
                    vecVariableNodes.Add(variableNode.strVarNodeName, variableNode);
                }
            }
        }

        public void readVecNodeFolder(XmlNode node, ref VariableNode variableNode, ref Dictionary<string, VariableNode> vecVariableNodes)
        {

            variableNode.strVarNodeName = node.Attributes["name"].Value;
            //Debug.Log(variableNode.strVarNodeName);
            
            variableNode.strVarNodeType = node.Attributes["step"].Value;
            variableNode.bFolder = true;
            for (var i = 0; i < node.ChildNodes.Count; i++)
            {
                VariableNode variableItem = new VariableNode();
                
                if (node.ChildNodes[i].Name == "VariableType")
                {
                    this.readVecNodeFolder(node.ChildNodes[i], ref variableItem, ref vecVariableNodes);
                }
                if (node.ChildNodes[i].Name == "Variable")
                {
                    this.readVarNodeItem(node.ChildNodes[i], ref variableItem, ref vecVariableNodes);
                }
                
                variableNode.vecItems.Add(variableItem);
                
            }
        }

        //读取VarialbeNode节点
        public void readVarNodeItem(XmlNode child, ref VariableNode variableNode, ref Dictionary<string, VariableNode> vecVariableNodes)
        {
            variableNode.strVarNodeName = child.SelectSingleNode("Name").InnerText;
            //Debug.Log("哈哈1:"+variableNode.strVarNodeName);
            variableNode.strVarNodeType = child.SelectSingleNode("Type").InnerText;

            //Debug.Log(variableNode.strVarNodeName);

            //int stepAttr = int.Parse(child.SelectSingleNode("Type").Attributes["step"].Value);// Convert.ToInt32(child.SelectSingleNode("Type").Attributes["step"].Value);
            int stepAttr = 0;
            int.TryParse(child.SelectSingleNode("Type").Attributes["step"].Value, out stepAttr);
            variableNode.dVarStep = stepAttr;

            var preConds = child.SelectSingleNode("PreConditions");
            if (preConds != null)
            {
                var preCond = preConds.SelectNodes("Condition");

                for (var i = 0; i < preCond.Count; i++)
                {
                    ConditionState conditionState = new ConditionState();
                    conditionState.strName = preCond[i].Attributes["name"].Value;

                    var strType = preCond[i].Attributes["type"].Value;
                    if (strType == "Greater")
                    {
                        conditionState.eEqualType = EqualType.Greater;
                    }
                    if (strType == "Less")
                    {
                        conditionState.eEqualType = EqualType.Less;
                    }
                    if (strType == "GreaterEqual")
                    {
                        conditionState.eEqualType = EqualType.GreaterEqual;
                    }
                    if (strType == "LessEqual")
                    {
                        conditionState.eEqualType = EqualType.LessEqual;
                    }
                    if (strType == "Equal")
                    {
                        conditionState.eEqualType = EqualType.Equal;
                    }
                    if (strType == "NotEqual")
                    {
                        conditionState.eEqualType = EqualType.NotEqual;
                    }

                    conditionState.cDestValue.dValue = int.Parse(preCond[i].Attributes["value"].Value)*1;

                    if (preCond[i].Attributes["time"] != null)
                    {
                        conditionState.cTime.dValue = int.Parse(preCond[i].Attributes["time"].Value)*1;
                    }
                    if (conditionState.strName != null && conditionState.strName != "")
                        variableNode.arrPreCondtion.Add(conditionState);

                }

            }


            double d_value = 0;
            var postConds = child.SelectSingleNode("PostConditions");
            if (postConds != null)
            {
                var postCond = postConds.SelectNodes("Condition");
                for (var i = 0; i < postCond.Count; i++)
                {
                    ConditionState conditionState = new ConditionState();
                    conditionState.strName = postCond[i].Attributes["name"].Value;
                    //conditionState.cDestValue = new ConditionValue(double.Parse(postCond[i].Attributes["value"].Value),/*Convert.ToDouble(postCond[i].Attributes["value"].Value),*/ postCond[i].Attributes["value"].Value);
                    
                    double.TryParse(postCond[i].Attributes["value"].Value, out d_value);
                    conditionState.cDestValue = new ConditionValue(d_value,/*Convert.ToDouble(postCond[i].Attributes["value"].Value),*/ postCond[i].Attributes["value"].Value);
                    conditionState.cTime = new ConditionValue(double.Parse(postCond[i].Attributes["time"].Value)/*Convert.ToDouble(postCond[i].Attributes["time"].Value)*/, postCond[i].Attributes["time"].Value);
                    conditionState.bJump = bool.Parse(postCond[i].Attributes["jump"].Value);// Convert.ToBoolean(postCond[i].Attributes["jump"].Value);
                    conditionState.bSync = bool.Parse(postCond[i].Attributes["sync"].Value);// Convert.ToBoolean(postCond[i].Attributes["sync"].Value);
                    conditionState.cStepValue = new ConditionValue(double.Parse(postCond[i].Attributes["stepValue"].Value)/*Convert.ToDouble(postCond[i].Attributes["stepValue"].Value)*/, postCond[i].Attributes["stepValue"].Value);
                    conditionState.cNotValue = new ConditionValue(double.Parse(postCond[i].Attributes["notValue"].Value)/*Convert.ToDouble(postCond[i].Attributes["notValue"].Value)*/, postCond[i].Attributes["notValue"].Value);
                    conditionState.cNotTime = new ConditionValue(double.Parse(postCond[i].Attributes["notTime"].Value)/*Convert.ToDouble(postCond[i].Attributes["notTime"].Value)*/, postCond[i].Attributes["notTime"].Value);

                    if (conditionState.strName != null && conditionState.strName != "")
                    {
                        variableNode.arrCondition.Add(conditionState);

                    }

                }
            }

            string strObjects = child.SelectSingleNode("ActiveObj").InnerText;
            if (strObjects != "")
            {
                var strSplits = strObjects.Split(',');
                variableNode.setActiveObj = new HashSet<string>(strSplits);
            }

            string strAnimaObjects = child.SelectSingleNode("AnimaObj").InnerText;
            if (strAnimaObjects != "")
            {
                variableNode.strAnimateObj = strAnimaObjects;
                variableNode.setAnimateObj = strAnimaObjects.Split(',');
            }

            variableNode.strVarTools = child.SelectSingleNode("Tools").InnerText;
            var strAssembleObjects = child.SelectSingleNode("Assemble").InnerText;
            variableNode.arrAssemble = strAssembleObjects.Split(',');

            variableNode.bVarLoop = bool.Parse(child.SelectSingleNode("AnimaLoop").InnerText);       
            variableNode.dVarStartTime = double.Parse(child.SelectSingleNode("StartTime").InnerText);
            variableNode.dVarStopTime = double.Parse(child.SelectSingleNode("StopTime").InnerText);  
            variableNode.dVarMinValue = double.Parse(child.SelectSingleNode("MinValue").InnerText);  
            variableNode.dVarMaxValue = double.Parse(child.SelectSingleNode("MaxValue").InnerText);  
            variableNode.bVarRandom = bool.Parse(child.SelectSingleNode("Random").InnerText);        
            variableNode.dVarCurValue = double.Parse(child.SelectSingleNode("CurValue").InnerText);  
            variableNode.strVarAudio = child.SelectSingleNode("Audio").InnerText;
            if (child.SelectSingleNode("Text") != null)
            {
                variableNode.strVarText = child.SelectSingleNode("Text").InnerText;
            }

            if (child.SelectSingleNode("PictureType") != null)
            {
                variableNode.strVarPictureType = child.SelectSingleNode("PictureType").InnerText;
            }
            if (child.SelectSingleNode("PicturePath") != null)
            {
                variableNode.strVarPicturePath = child.SelectSingleNode("PicturePath").InnerText;
            }

            if (child.SelectSingleNode("Assess") != null)
            {
                variableNode.bVarAssess = bool.Parse(child.SelectSingleNode("Assess").InnerText);
            }

            variableNode.strVarViewport = child.SelectSingleNode("ViewPort").InnerText;

            var strOrder = child.SelectSingleNode("OrderCon").InnerText;
            variableNode.arrOrder = strOrder.Split(',');
            {
                var strPepole = child.SelectSingleNode("People").InnerText;

                List<string> arrStrPepole = new List<string>();
                arrStrPepole = new List<string>(strPepole.Split(','));
                //for (int i = 0; i < arrStrPepole.Count; i++)
                    //variableNode.arrPeople.Add(int.Parse(arrStrPepole[i])/*Convert.ToDouble(arrStrPepole[i])*/);
            }

            var strWarn = child.SelectSingleNode("Warn").InnerText;
            variableNode.arrErrorInfo = strWarn.Split(',');

            if (!vecVariableNodes.ContainsKey(variableNode.strVarNodeName))
            vecVariableNodes.Add(variableNode.strVarNodeName, variableNode);
        }

        //读取StateType分割的VarialeState
        public void readVariableStateFolder(XmlNode node, VariableState variableState)
        {
            variableState.strFactorName = node.Attributes["name"].Value;
            variableState.bFolder = true;
            var child = node.ChildNodes;
            for (var i = 0; i < child.Count; i++)
            {
                VariableState varState;
                varState.strDisp = "";
                varState.strType = "";
                varState.strFactorName = "";
                varState.bFolder = false;
                varState.vecItems = new List<VariableState>();

                varState.arrAction = new List<ConditionState>();
                varState.arrCondition = new List<ConditionState>();
                varState.arrPreState = new List<ConditionState>();

                varState.dFactor = 0.0;
                if (child[i].Name == "StateType")
                {
                    this.readVariableStateFolder(child[i], varState);
                }
                if (child[i].Name == "State")
                {
                    this.readVariableStateItem(child[i], varState);
                }
                variableState.vecItems.Add(varState);
            }
        }

        //读取VariableState节点
        public void readVariableStateItem(XmlNode child, VariableState varState)
        {
            varState.strType = child.Attributes["type"].Value;
            varState.strFactorName = child.Attributes["name"].Value;
            varState.dFactor = double.Parse(child.Attributes["factor"].Value);
            varState.strDisp = child.Attributes["Disp"].Value;

            //前置条件;
            var preConds = child.SelectSingleNode("PreConditions");
            if (preConds != null)
            {
                var preCond = preConds.SelectNodes("PreState");

                for (var i = 0; i < preCond.Count; i++)
                {
                    ConditionState conditionState = new ConditionState();
                    conditionState.strName = preCond[i].Attributes["name"].Value;

                    var strType = preCond[i].Attributes["type"].Value;
                    if (strType == "Greater")
                    {
                        conditionState.eEqualType = EqualType.Greater;
                    }
                    if (strType == "Less")
                    {
                        conditionState.eEqualType = EqualType.Less;
                    }
                    if (strType == "GreaterEqual")
                    {
                        conditionState.eEqualType = EqualType.GreaterEqual;
                    }
                    if (strType == "LessEqual")
                    {
                        conditionState.eEqualType = EqualType.LessEqual;
                    }
                    if (strType == "Equal")
                    {
                        conditionState.eEqualType = EqualType.Equal;
                    }
                    if (strType == "NotEqual")
                    {
                        conditionState.eEqualType = EqualType.NotEqual;
                    }

                    var strLogic = preCond[i].Attributes["logic"].Value;
                    if (strLogic == "Or")
                    {
                        conditionState.eLogicType = LogicType.Or;
                    }
                    else if (strLogic == "And")
                    {
                        conditionState.eLogicType = LogicType.Add;
                    }
                    else if (strLogic == "Sub")
                    {
                        conditionState.eLogicType = LogicType.Sub;
                    }

                    conditionState.cDestValue.dValue = double.Parse(preCond[i].Attributes["value"].Value);
                    if (conditionState.strName != null && conditionState.strName != "")
                    {
                        varState.arrPreState.Add(conditionState);
                    }
                }
            }

            //条件;
            var cndNodes = child.SelectSingleNode("Conditions");
            if (cndNodes != null)
            {
                var conChild = cndNodes.SelectNodes("Condition");
                for (var j = 0; j < conChild.Count; j++)
                {
                    ConditionState conditionState = new ConditionState();

                    conditionState.strName = conChild[j].Attributes["name"].Value;
                    var strType = conChild[j].Attributes["type"].Value;
                    if (strType == "Greater")
                    {
                        conditionState.eEqualType = EqualType.Greater;
                    }
                    if (strType == "Less")
                    {
                        conditionState.eEqualType = EqualType.Less;
                    }
                    if (strType == "GreaterEqual")
                    {
                        conditionState.eEqualType = EqualType.GreaterEqual;
                    }
                    if (strType == "LessEqual")
                    {
                        conditionState.eEqualType = EqualType.LessEqual;
                    }
                    if (strType == "Equal")
                    {
                        conditionState.eEqualType = EqualType.Equal;
                    }
                    if (strType == "NotEqual")
                    {
                        conditionState.eEqualType = EqualType.NotEqual;
                    }

                    var strLogic = conChild[j].Attributes["logic"].Value;
                    if (strLogic == "Or")
                    {
                        conditionState.eLogicType = LogicType.Or;
                    }
                    else if (strLogic == "Add")
                    {
                        conditionState.eLogicType = LogicType.Add;
                    }
                    else if (strLogic == "Sub")
                    {
                        conditionState.eLogicType = LogicType.Sub;
                    }

                    conditionState.cDestValue.dValue = double.Parse(conChild[j].Attributes["value"].Value);
                    if (conditionState.strName != null && conditionState.strName != "")
                    {
                        varState.arrCondition.Add(conditionState);
                    }


                }
            }

            var actionNodes = child.SelectSingleNode("Actions");
            if (actionNodes != null)
            {
                var actionChild = actionNodes.SelectNodes("Action");
                for (var k = 0; k < actionChild.Count; k++)
                {
                    ConditionState conditionState = new ConditionState();
                    conditionState.strName = actionChild[k].Attributes["name"].Value;
                    conditionState.cDestValue = new ConditionValue(double.Parse(actionChild[k].Attributes["value"].Value), actionChild[k].Attributes["value"].Value);
                    conditionState.cNotValue = new ConditionValue(double.Parse(actionChild[k].Attributes["notValue"].Value), actionChild[k].Attributes["notValue"].Value);
                    conditionState.cStepValue = new ConditionValue(double.Parse(actionChild[k].Attributes["stepValue"].Value), actionChild[k].Attributes["stepValue"].Value);
                    conditionState.cTime = new ConditionValue(double.Parse(actionChild[k].Attributes["time"].Value), actionChild[k].Attributes["time"].Value);
                    conditionState.cNotTime = new ConditionValue(double.Parse(actionChild[k].Attributes["notTime"].Value), actionChild[k].Attributes["notTime"].Value);
                    conditionState.nLeavel = int.Parse(actionChild[k].Attributes["level"].Value);
                    conditionState.bJump = bool.Parse(actionChild[k].Attributes["jump"].Value);
                    conditionState.bSync = bool.Parse(actionChild[k].Attributes["sync"].Value);

                    if (/*conditionState.strName != undefined &&*/ conditionState.strName != "")
                    {
                        varState.arrAction.Add(conditionState);
                    }
                }
            }
        }

        //读取vairalbeState节点
        public void readVarialbeStates(XmlDocument xmlDocument, List<VariableState> arrVariableStates)
        {
            var parentNodes = xmlDocument.SelectSingleNode("Sence/VariableStates");
            if (parentNodes != null)
            {
                var child = parentNodes.ChildNodes;
                for (var i = 0; i < child.Count; i++)
                {
                    VariableState variableState;
                    variableState.strDisp = "";
                    variableState.strType = "";
                    variableState.strFactorName = "";
                    variableState.bFolder = false;
                    variableState.vecItems = new List<VariableState>();

                    variableState.arrAction = new List<ConditionState>();
                    variableState.arrCondition = new List<ConditionState>();
                    variableState.arrPreState = new List<ConditionState>();

                    variableState.dFactor = 0.0;

                    if (child[i].Name == "StateType")
                    {
                        this.readVariableStateFolder(child[i], variableState);
                    }

                    if (child[i].Name == "State")
                    {
                        this.readVariableStateItem(child[i], variableState);

                    }
                    arrVariableStates.Add(variableState);
                }
            }
        }

        public List<ScenePhotoDome> readSceneDomes(XmlDocument xmlDocument)
        {
            List<ScenePhotoDome> photodomes = new List<ScenePhotoDome>();
            var domesNode = xmlDocument.SelectSingleNode("Sence/PhotoDomes");

            if (domesNode != null)
            {
                var domeNode = domesNode.SelectNodes("PhotoDome");
                for (int i = 0; i < domeNode.Count; i++)
                {
                    ScenePhotoDome scenesDome;
                    scenesDome.strImg = "";

                    scenesDome.strImg = domeNode[i].Attributes["img"].Value;

                    photodomes.Add(scenesDome);
                }
            }

            return photodomes;
        }
    }
}
